package com.ywu.bpmn.flow.consumer;

import com.ywu.bpmn.flow.condfig.YwuBpmnFlowConsumerConfig;
import com.ywu.mq.sdk.consumer.YwuKafkaConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName YwuBpmnFlowConsumer
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/4/23 20:39
 * @Version 1.0
 **/
@Component
public class YwuBpmnFlowConsumer extends YwuKafkaConsumer {
    @Autowired
    private List<YwuFlowConsumer> consumers;

    private Map<String, YwuFlowConsumer> consumerMap;

    @Autowired
    private YwuBpmnFlowConsumerConfig config;

    @PostConstruct
    public void init() {
        init(config);
    }

    @Override
    public void doCustomConsumer(String key, String value) {
        if (Objects.isNull(consumerMap)) {
            consumerMap = consumers.stream().collect(Collectors.toMap(YwuFlowConsumer::type, res -> res, (v1, v2) -> v1));
        }
        if (consumerMap.containsKey(key)) {
            YwuFlowConsumer ywuFlowConsumer = consumerMap.get(key);
            ywuFlowConsumer.process(key, value);
        }
    }
}
